package model

import (
	"gorm.io/gorm"
	"seckill-srv/basic/config"
)

type User struct {
	gorm.Model
	Mobile   string `json:"mobile" gorm:"type:char(11);unique;comment:'手机号'"`
	Password string `json:"password" gorm:"type:char(32);comment:'密码'"`
	Status   int    `json:"status" gorm:"type:int;comment:'状态 1-正常 2-禁用'"`
}

func (u *User) TableName() string {
	return "user"
}

// 用户注册
func (u *User) AddUser() error {
	return config.DB.Create(&u).Error
}

// 根据mobile查询用户
func (u *User) GetUserByMobile(mobile string) error {
	return config.DB.Model(&User{}).Where("mobile = ?", mobile).Limit(1).Find(&u).Error
}

// 根据id查询用户
func (u *User) GetUserById(userId int) error {
	return config.DB.Model(&User{}).Where("id = ?", userId).Limit(1).Find(&u).Error
}
